from tortoise import fields
import datetime
from models.base import *


class Account(TimestampMixin, OtherMixin, UidMixin, Model):
    nickname = fields.CharField(max_length=50, default="nick name")
    name = fields.CharField(max_length=50, description="user name")
    mobile = fields.CharField(max_length=50, description="user mobile")
    password = fields.CharField(max_length=64, default=" password ")
    state = fields.IntField(description="1 default", default=0)
    activate = fields.IntField(description="activate 1", default=0)

    class Meta:
        abstract = False
        table = 'a_account'
        table_description = '用户表'

    async def list_to_dict(self):
        request_dict = {f: getattr(self, f) for f in self._meta.fields}
        request_dict['create_ts'] = datetime.datetime.strftime(request_dict['create_ts'], '%Y-%m-%d')
        request_dict['update_ts'] = datetime.datetime.strftime(request_dict['update_ts'], '%Y-%m-%d')
        return request_dict

    async def to_redis_dict(self):
        request_dict = {f: getattr(self, f) for f in self._meta.fields}
        request_dict['create_ts'] = datetime.datetime.strftime(request_dict['create_ts'], '%Y-%m-%d')
        request_dict['update_ts'] = datetime.datetime.strftime(request_dict['update_ts'], '%Y-%m-%d')
        del request_dict['password']
        return request_dict

    async def to_jwt_dict(self):
        request_dict = {f: getattr(self, f) for f in self._meta.fields}
        res_dict = {"sub": request_dict.get("mobile"),
                    "uid": request_dict.get("uid")}

        return res_dict
